// service/serveItem/serveItem.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    cheType: [],
    cate: [],
    category_id: '',
    goods: [],
    currentGoods: [],
    page: 1,
    finish: false,
    loading: false,
    list: [],
    listNum: 0,
    skill_cate_id: '',
    safeBottom: `30px`,
    city: '',
    token: '',
    cateCheckAll: false
  },
  detail(e) {
    let id = e.currentTarget.dataset.id;
    util.skip('/service/servedetail/servedetail?id=' + id)
  },
  select(e) {
    let item = e.currentTarget.dataset.item
    if (item.select === 0) {
      return
    }
    let id = e.currentTarget.dataset.item.id
    let index = this.data.goods.indexOf(id)
    let goods = this.data.goods;

    let currentGoods = [];
    // let currentGoodsIndex = currentGoods.indexOf(id)
    // if (currentGoodsIndex === -1) {
    //   currentGoods.push(id)
    // } else {
    //   currentGoods.splice(currentGoodsIndex, 1)
    // }
    let add = false
    if (index === -1) {
      goods.push(id)
      add = true
    } else {
      goods.splice(index, 1)
      add = false
    }
    let arr = this.data.list.map(value => {
      if (value.id === id) {
        value.select = add ? 2 : 1
      }
      return value
    })
    currentGoods = arr.filter(i => i.select == 2).map(i => i.id)
    this.setData({
      goods,
      list: arr,
      listNum: arr.filter(item => item.select !== 0).length,
      currentGoods,
      cateCheckAll: arr.filter(item => item.select !== 0).length > 0 && arr.filter(item => item.select !== 0).length === currentGoods.length,

    })

  },
  onChange(e) {
    this.setData({
      category_id: this.data.cate[e.detail.index].id,
      cateCheckAll: false,
      currentGoods: []
    })
    this.reload()
  },
  reload() {
    this.setData({
      list: [],
      currentGoods: [],
      listNum: 0,
      page: 1,
      finish: false
    })
    this.getList()
  },
  more() {
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  back() {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('selectGood', {
      goods: this.data.goods
    });
    util.back()
  },
  getList() {
    if (this.data.finish) {
      return
    }
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true
    })
    http.get('goods/getapplygoods', {
      type: 0,
      city: this.data.city,
      category_id: this.data.category_id,
      page: this.data.page
    }, true, false, this.data.token).then(res => {
      if (res.data.length === 0) {
        this.setData({
          finish: true
        })
      }
      let currentGoods = []
      let data = res.data.map(value => {
        value.skill_cate_ids = value.skill_cate_ids.split(',')
        console.log(value.skill_cate_ids)
        console.log(this.data.skill_cate_id)
        console.log(this.data.goods)
        if (value.skill_cate_ids.every(item1 =>
            !this.data.skill_cate_id.some(item2 => item2 == item1)
          )) {
          value.select = 0
        } else if (this.data.goods.indexOf(value.id) === -1) {
          value.select = 1
        } else {
          value.select = 2
          // currentGoods.push(value.id)
        }
        return value
      })
      let arr = this.data.list.concat(data)
      currentGoods = arr.filter(i => i.select == 2).map(i => i.id)
      this.setData({
        list: arr,
        listNum: arr.filter(item => item.select !== 0).length,

        currentGoods,
        cateCheckAll: arr.filter(item => item.select !== 0).length > 0 && arr.filter(item => item.select !== 0).length === currentGoods.length
      })

    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },

  getInfo() {
    http.post('category/getlist', '', true, false, this.data.token).then(res => {
      this.setData({
        cate: res.data,
        category_id: res.data[0].id
      })
      this.getList()
    })
  },
  onLoad(options) {
    const high = app.globalData.safeBottom
    this.setData({
      safeBottom: `${high}px`
    })
    let arr = options.goods_ids ? options.goods_ids.split(',') : []
    let goods = arr.map(value => {
      return Number(value)
    })

    this.setData({
      skill_cate_id: options.skill ? options.skill.split(',') : [],
      goods: goods,
      city: options.city,
      token: options.token || ''
    })

    this.getInfo()
  },


  cateCheckAllToggle() {
    if (this.data.listNum === 0) {
      return
    }
    const checkbox = this.selectComponent(`.checkbox-all`);
    console.log(checkbox)
    checkbox.toggle();
  },
  cateCheckAllChange(e) {
    const {
      list,
      goods,
      currentGoods
    } = this.data;
    let data = list.filter(item => item.select != 0)
    let selectValue = []
    let listNew = list.map(item => {
      if (item.select != 0) {
        item.select = e.detail ? 2 : 1
        let index = goods.indexOf(item.id)
        if (e.detail) {
         
          if (index === -1) {
            goods.push(item.id)
          }
        } else {
          
          if (index !== -1) {
            goods.splice(index, 1)
          }
        }
      }
      return item
    })
    if (e.detail) {
      selectValue = listNew.map(item => {
        return item.id
      })
    }
    this.setData({
      cateCheckAll: e.detail,
      currentGoods: selectValue,
      list: listNew,
      goods,
      listNum: listNew.filter(item => item.select !== 0).length,
    });
  },

  noop() {
    return
  }
})